<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">简历投递记录</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>简历投递记录</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <select id="resumerecord-search-key">
                <option value="">-请选择-</option>
                <option value="id">Id</option>
                <option value="addresser">投递者</option>
                <option value="recipitien">公司</option>
            </select>&emsp;
            <input id="resumerecord-search-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <div class="layui-inline"> <!-- 注意：这一层元素并不是必须的 -->
                <input type="text" placeholder="请选择开始时间" class="layui-input" id="start">
            </div>
            <div class="layui-inline"> <!-- 注意：这一层元素并不是必须的 -->
                <input type="text" placeholder="请选择结束时间" class="layui-input" id="end">
            </div>
            <button id="resumerecord-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
<!--            <button id="resumerecord-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>-->
            <button id="resumerecord-btn-export" class="layui-btn icon-btn"><i class="layui-icon">&#xe601;</i>导出</button>
        </div>

        <table class="layui-table" id="resumerecord-table" lay-filter="resumerecord-table"></table>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="resumerecord-table-bar">
<!--    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>-->
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="delete">删除</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="exprot-one">下载简历</a>
    <!--    <a class="layui-btn layui-btn-xs" lay-event="reset">重置密码</a>-->
</script>

<script>
    layui.use(['form', 'table', 'util', 'config', 'admin','laydate'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#start' //指定元素
            ,type:'datetime'
            ,format:'yyyy-MM-dd HH:mm:ss'
        });
        laydate.render({
            elem: '#end' //指定元素
            ,type:'datetime'
            ,format:'yyyy-MM-dd HH:mm:ss'
        });
        //if( admin.hasPerm('user:add') ) {
        //   $('#btnAdd').hide();
        //}

        // 添加按钮绑定权限
        if(!admin.hasPerm('resume:saveOrUpdate')) {
            $('#resumerecord-btn-add').hide();
        }
        console.log("12131")
        // 导出按钮绑定权限
        if(!admin.hasPerm('resume:saveOrUpdate')) {
            $('#resumerecord-btn-export').hide();
        }


        console.log(config.getUser().id)
        // 渲染表格
        var tableIns=table.render({
            elem: '#resumerecord-table',
            // url: config.base_server + 'user_query.json',
            url: config.base_server + 'api-user/interResume',
            method: 'GET',
            where: {
                access_token: config.getToken().access_token,
                userId:config.getUser().id
            },
            response:{
                statusName: 'code',
                statusCode:200
            },
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'id', sort: true, title: 'ID',width: 50},
                {field: 'resumeId', sort: true,width: 80, title: '简历ID',templet:function(data){
                    return data.resume==null?'':data.resume.id;
                    }},
                {field: 'activity', sort: true,width: 80, title: '活动名称',templet:function(data){
                    return data.activity==null?'':data.activity.title;
                    }},
                {field: 'resumeName', sort: true, title: '简历名称',templet:function(data){
            return data.resume==null?'':data.resume.name;
                }},
                {field: 'companyName', sort: true, title: '企业名称',templet:function(data){
                        return data.company==null?'':data.company.companyName;
                    }},
                {field: 'addresserName', sort: true, title: '投递人',templet:function(data){
                        return data.resume==null || data.resume.addresser==null?'':data.resume.addresser.username;
                    }},
                {field:'createTime',
                    sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {align: 'center', toolbar: '#resumerecord-table-bar', title: '操作',width: 150}
            ]]
        });

        // 添加按钮点击事件
        $('#resumerecord-btn-add').click(function () {
            showEditModel();
        });

        // 工具条点击事件
        table.on('tool(resumerecord-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
                // showEditModel(data);
            }
            else if (layEvent === 'delete') { // 重置密码
                layer.confirm('确定删除吗？', function (i) {
                    layer.close(i);
                    layer.load(2);
                    admin.req('api-user/interResume/'+obj.data.id, {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 0) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                            // tableIns()
                            table.reload('resumerecord-table');
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }

                    }, 'DELETE');
                });
            }else if(layEvent==='exprot-one'){
                console.log(data.resume.id)
                var token = config.getToken();
                location.href=config.base_server+'api-user/resume/download?access_token='+token.access_token+'&id='+data.resume.id;
            }
        });

        //显示表单弹窗
        // var showEditModel = function (data) {
        //     admin.putTempData('t_resume', data);
        //     var title = data ? '修改简历' : '添加简历';
        //     admin.popupCenter({
        //         title: title,
        //         path: 'pages/system/resume_form.html',
        //         finish: function () {
        //             table.reload('resumerecord-table', {});
        //         }
        //     });
        // };

        // 搜索按钮点击事件
        $('#resumerecord-btn-search').click(function () {
            var key = $('#resumerecord-search-key').val();
            var value = $('#resumerecord-search-value').val()+"";
            table.reload('resumerecord-table', {where: {userId:config.getUser().id,searchKey: key, searchValue: value,end:$("#end").val(),start:$("#start").val()}});
        });


        // 导出
        $('#resumerecord-btn-export').click(function () {
            console.log("导出")
            // var xhr = new XMLHttpRequest();
            // xhr.open('POST', url, true);
            // xhr.responseType = "blob";
            // xhr.setRequestHeader("client_type", "DESKTOP_WEB");
            // xhr.onload = function() {
            //     if (this.status == 200) {
            //         var blob = this.response;
            //         var objecturl = URL.createObjectURL(blob);
            //         window.location.href = objecturl;
            //     }
            // }
            // xhr.send();
            var key = $('#resumerecord-search-key').val();
            var value = $('#resumerecord-search-value').val()+"";
            console.log("exports")
            admin.req('api-user/interResume'
                ,{searchKey: key, searchValue: value,end:$("#end").val(),start:$("#start").val(),page:0,limit:1000}
                ,(res)=>{
                    if(res.code===200){
                        var list=new Array()
                        $.each(res.data,(i,data)=>{
                            var da=new Object();
                            da.id=data.id
                            da.resumeId=data.resume==null?'':data.resume.id
                            da.resumeName=data.resume==null?'':data.resume.name
                            da.companyName=data.company==null?'':data.company.compayName
                            da.createTime=data.createTime
                            da.addresserName=data.resume==null || data.resume.addresser==null?'':data.resume.addresser.username
                            data.activity=data.activity==null?'':data.activity.title
                            list.push(da)
                        })
                        table.exportFile(tableIns.config.id
                            ,list
                            ,'xls');
                    }else {
                        layer.msg(res.msg)
                    }

                }
                ,'GET')
        });

    });
</script>